package icu.ac.kr.simpleRssReader.activity;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


import icu.ac.kr.simpleRssReader.*;
import icu.ac.kr.simpleRssReader.provider.RssDBProviderConstant;
import icu.ac.kr.simpleRssReader.view.ChannelHead;
import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewPost extends Activity {

	static final String TAG = "ViewPost";

	static final int NORMALGROUP = 1;
	static final int BACKTOCHANNEL = 1;
	static final int BACKTOPOST = 2;
	
	static long channelId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewpost);
		
		Intent intent = getIntent();
		Uri uri = null;
		if (intent != null)
			uri = intent.getData();
		
		Log.d(TAG, uri.toString());
		ContentValues values = new ContentValues();
		values.put("read", 1);
		
		getContentResolver().update(uri, values,"_id ="+uri.getPathSegments().get(1), null);
		Cursor cursor = getContentResolver().query(uri, new String[]{"_id","channel_id", "title", "body"}, "_id ="+uri.getPathSegments().get(1), null, null);
		
		TextView title = (TextView) findViewById(R.id.title);
		TextView description = (TextView) findViewById(R.id.description);
		
		cursor.moveToFirst();
		channelId = cursor.getLong(cursor.getColumnIndex(RssDBProviderConstant.post.CHANNELID));
		
		ChannelHead header = (ChannelHead)findViewById(R.id.chHeader);
		header.bindData(""+channelId);
		title.setTypeface(Typeface.DEFAULT_BOLD);
		description.setTypeface(Typeface.DEFAULT_BOLD);
		title.setText("Title: "+cursor.getString(cursor.getColumnIndex(RssDBProviderConstant.post.TITLE)));
		description.setText("Description: ");
 		String body =cursor.getString(cursor.getColumnIndex(RssDBProviderConstant.post.BODY));
		WebView postWeb = (WebView)findViewById(R.id.postWeb);

		/* TODO: I want the background transparent, but that doesn't seem 
		 * possible.  Black will do for now. */
		String html =
		  "<html><head><style type='text/css'>body { background-color: #201c19; color: white; } a { color: #ddf; }</style></head><body>" +
		  body +
		  "<a href='http://www.google.com'>more</a></body></html>";

		postWeb.loadData(html, "text/html", "utf-8");
		cursor.close();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
	
		menu.add(NORMALGROUP, BACKTOCHANNEL, 0, "Back to Channel List");
		menu.add(NORMALGROUP, BACKTOPOST, 0, "Back to Post List");
		
		return true;
	
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		super.onMenuItemSelected(featureId, item);
	
		switch(item.getItemId())
		{
		case BACKTOCHANNEL:
			finish();
			break;
			
		case BACKTOPOST:
			startActivity(new Intent(Intent.ACTION_VIEW, ContentUris.withAppendedId(RssDBProviderConstant.CHANNEL_URI, channelId)));
			finish();
			break;
		}
		return true;
	}

	
	
	
	
}
